import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Sorts;
import com.movie.recomend.Application;
import com.movie.recomend.controller.CNMovieRestApi;
import com.movie.recomend.controller.MainController;
import com.movie.recomend.model.domain.Movie;
import com.movie.recomend.model.domain.Rating;
import com.movie.recomend.model.recom.Recommendation;
import com.movie.recomend.model.request.MovieRecommendationRequest;
import com.movie.recomend.model.request.RateMoreRecommendationRequest;
import com.movie.recomend.model.request.UserRecommendationRequest;
import com.movie.recomend.service.MovieService;
import com.movie.recomend.service.RecommendService;
import com.movie.recomend.utils.Constant;
import org.bson.Document;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Devhui
 * @Date: 2020/8/11 19:03
 * @Email: devhui@ihui.ink
 * @Version: 1.0
 */

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class TEST {

    @Autowired
    private MainController mainController;

    @Test
    public void getMain() {
        mainController.test();
    }

    @Autowired
    private RecommendService recommendService;

    @Test
    public void getMyRateMovies() {
        int mid = 2545;
//        String _id  = "5f2d09ed8fa21e1cf827faf5";
//        Query query = new Query(Criteria.where("mid").is(mid));
//        Movie movies = mongoTemplate.findOne(query, Movie.class);
//        Movie byId = mongoTemplate.findById(_id, Movie.class, "Movie");
//        System.out.println("movies = " + byId);
    }

    @Test
    public void getHotMovies() {
//        RateMoreRecommendationRequest rateMoreRecommendationRequest = new RateMoreRecommendationRequest(3);
//
//        List<Recommendation> rateMoreRecommendations = movieService.getRateMoreRecommendations(rateMoreRecommendationRequest);
//        System.out.println("rateMoreRecommendations = " + rateMoreRecommendations);
    }


    @Autowired
    private MongoClient mongoClient;

    @Test
    public void testMongoClient() {
        MongoCollection<Document> collection = mongoClient.getDatabase(Constant.MONGODB_DATABASE).getCollection(Constant.MONGODB_RATING_COLLECTION);
        FindIterable<Document> documents = collection.find().sort(Sorts.descending("yearmonth")).limit(3);

        List<Rating> recommendations = new ArrayList<>();
        for (Document document : documents) {
            recommendations.add(new Rating(document.getInteger("uid"), document.getInteger("mid"),
                    document.getDouble("score")));
        }
        System.out.println("recommendations = " + recommendations);
    }


    @Test
    public void getContentLikeMovies() {
        MovieRecommendationRequest movieRecommendationRequest = new MovieRecommendationRequest(1084, 3);
        List<Recommendation> contentBasedMoreLikeThisRecommendations = recommendService.getContentBasedMoreLikeThisRecommendations(movieRecommendationRequest);
        System.out.println("contentBasedMoreLikeThisRecommendations = " + contentBasedMoreLikeThisRecommendations);
    }


    @Test
    public void getGenres() {
        List topGenres = recommendService.getTopGenres();
        System.out.println("topGenres = " + topGenres);
    }


    @Autowired
    private MovieService movieService;

    @Autowired
    private CNMovieRestApi cnMovieRestApi;

    @Test
    public void getALS_CN() {
        List c = cnMovieRestApi.CN("c", 4);
        System.out.println("c = " + c);
    }

}
